/*
 * @(#)AtnaDaemon.java
 * Date 2013-10-31
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.atna;

import javax.enterprise.event.Observes;

import javax.inject.Inject;

import com.nesscomputing.syslog4j.server.SyslogServerEventHandlerIF;

import com.nesscomputing.syslog4j.server.impl.event.printstream
    .SystemOutSyslogServerEventHandler;

import org.jboss.weld.environment.se.events.ContainerInitialized;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.atna.receiver.AuditEventHandler;
import us.hornerscorners.lamppost.atna.receiver.IAuditEventReceiver;

import us.hornerscorners.lamppost.atna.service.IAuditEventService;

import us.hornerscorners.lamppost.ihecommon.config.AtnaConfig;
import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-10-31
 * @author         Jim Horner
 */
public class AtnaDaemon {

    /** Field description */
    @Inject
    private IHEConfig config;

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /** Field description */
    @Inject
    private IAuditEventReceiver receiver;

    /** Field description */
    @Inject
    private IAuditEventService service;

    /**
     * Method description
     *
     *
     * @param event
     */
    public void main(@Observes ContainerInitialized event) {

        AtnaConfig atnaConfig = this.config.getAtnaConfig();

        if (atnaConfig.isServiceEnabled() == false) {

            String msg = "NOT STARTING. ATNA Service is disabled.";

            logger.error(msg);

            throw new IllegalStateException(msg);
        }

        AuditEventHandler auditEventHandler = new AuditEventHandler(this.service);

        this.receiver.addEventHandler(auditEventHandler);

        if (atnaConfig.isDebugMode()) {

            SyslogServerEventHandlerIF eventHandler =
                SystemOutSyslogServerEventHandler.create();

            this.receiver.addEventHandler(eventHandler);
        }

        this.receiver.startup();
    }
}
